//
//  AYNewFeatureCell.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/15.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit

class AYNewFeatureCell: UICollectionViewCell {

    
    
    //标记第几页
    var index: Int = 0 {
        didSet{
            bkgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            
            startButton.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        preparUI()
    }
    
    
    ///布局UI
    private func preparUI(){
        
        //添加内容控件
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startButton)
        
        //添加约束
        
        bkgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(self.contentView)
        }
        
        startButton.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView.snp_bottom).offset(-160)
        }
    }
    
    
    func startAnimation() {
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { 
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            }, completion: nil)
    }
    
    // MARK: - action
    ///开始按钮点击事件
    func starButtonClick() {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        appDelegate.switchRootViewController(AYMainViewController())
    }
    
    // MARK: - lazy
    
    ///背景图
    private lazy var bkgImageView: UIImageView = UIImageView( image: UIImage(named: "new_feature_1"))
    
    ///按钮
    private lazy var startButton: UIButton = {
       let button = UIButton()
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: #selector(starButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    
}
